package beans;

import menus.MenuManagedBean;
import lecture.LectureManagedBean;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import lecture.Lecture;
import lecture.LectureAdminManagedBean;
import javax.faces.bean.CustomScoped;
import javax.faces.context.FacesContext;
import user.User;

/**
 * Hold all information needed through the whole user session.
 * Note: In this case a session ends when the browser is closed,
 * different to the "normal" Wterminology 
 * @author Thorsten Ludwig
 */

@ManagedBean(name="sessionBean")
@CustomScoped(value="#{window}")
public class SessionManagedBean {
    /**
     * the current logged in user
     */
    private User user;
    /**
     * list of lectures where a user take part
     */
    private List<Lecture> lectures;
    /**
     * all current lectures
     */
    private List<Lecture> avaibleLectures;
    /**
     * 
     */
    private List<Lecture> administratedLectures;

    private boolean forwarded;
    /**
     *
     */
    @ManagedProperty(value="#{menuBean}")
    private MenuManagedBean menu;

    @ManagedProperty(value="#{lectureAdmin}")
    private LectureAdminManagedBean lectureAdmin;
    
    @ManagedProperty(value="#{lectureBean}")
    private LectureManagedBean lectureBean;
    /**
     *
     */
    public SessionManagedBean() {
        this.lectures = new ArrayList<Lecture>();
        this.avaibleLectures = new ArrayList<Lecture>();
        this.administratedLectures = new ArrayList<Lecture>();
    }

    /**
     *
     * @return
     */
    public User getUser() {
        return user;
    }

    /**
     *
     * @param user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     *
     * @return
     */
    public MenuManagedBean getMenu() {
        return menu;
    }

    /**
     *
     */
    public void setMenu(MenuManagedBean menu) {
        this.menu = menu;
    }

    public LectureAdminManagedBean getLectureAdmin() {
        return lectureAdmin;
    }

    public void setLectureAdmin(LectureAdminManagedBean lectureAdmin) {
        this.lectureAdmin = lectureAdmin;
    }
    
    /**
     * delete all entries when a user is logged out
     * @return the start page
     */
    public String doLogout() {
        System.out.println("doLogout was called");
        this.administratedLectures.clear();
        this.avaibleLectures.clear();
        this.lectures.clear();
        this.user = null;
        //this.menu = null;

        return "index.jsf";
    }

    /**
     * set up all lists to create a new menu
     */
    public void init() {
        System.out.println("init menu Bar");
        
        this.lectures = this.lectureAdmin.getLecturesByUser(this.user);
        this.menu.addLectures(lectures);

        this.avaibleLectures = this.lectureAdmin.getLectures();
        this.menu.addAvaibleLectures(this.avaibleLectures);

        this.administratedLectures = this.lectureAdmin.getLecturesByName(this.user.getIsAdminOf());
        this.menu.addAdminLectures(this.administratedLectures);
    }

    /**
     * 
     * @param string 
     */
    public void setSelectedLecture(String string) {
        this.lectureBean.setLecture(this.lectureAdmin.getLectureById(string));        
    }

    public LectureManagedBean getLectureBean() {
        return lectureBean;
    }

    public void setLectureBean(LectureManagedBean lectureBean) {
        this.lectureBean = lectureBean;
    }
    
    public boolean isLegalURL() {
        if (this.forwarded) {
            this.forwarded = false;
            return true;
        }
        FacesContext c = FacesContext.getCurrentInstance();
        String url = c.getViewRoot().getViewId();
        url = url.replace(".xhtml", "").substring(1);
        if (this.lectureBean.getSelectedSubCategory() == null)
            return false;
        String selected = this.lectureBean.getSelectedSubCategory().getLink();
        selected = selected.replace(".jsf", "");
        return (url.equalsIgnoreCase(selected) || 
                true ||
                
                url.equalsIgnoreCase("lecture") ||
                url.contains("staticPage"));
    }

    public boolean isForwarded() {
        return forwarded;
    }

    public void setForwarded(boolean forwarded) {
        this.forwarded = forwarded;
    }
}
